import EventPackage.Event;
import EventPackage.EventManagerInterface;
import TermioPackage.Termio;

public class SecuritySensorConsole extends Thread{

    private EventManagerInterface em;
    private String typeOfIntrusion;
    private boolean done = false;
    private boolean closing = false;

    public SecuritySensorConsole(EventManagerInterface em, String typeOfIntrusion) {
        this.em = em;
        this.typeOfIntrusion = typeOfIntrusion;
    }

    public void run () {
        Termio userInput = new Termio();
        String option;

        while (!done) {
            // User menu to insert the sensors states

            System.out.println( "\n\n\n\n" );

            System.out.println( "Window Security Sensors Simulation Console \n" );
            System.out.println( "Select an Option: \n" );
            if(typeOfIntrusion.equalsIgnoreCase("window")){
                System.out.println( "1: Simulate Window Breach" );
            }
            if(typeOfIntrusion.equalsIgnoreCase("door")){
                System.out.println( "1: Simulate Door Breach" );
            }
            if(typeOfIntrusion.equalsIgnoreCase("motion")){
                System.out.println( "1: Simulate Motion Detection" );
            }
            System.out.println( "X: Exit" );
            System.out.print( "\n>>>> " );
            option = userInput.KeyboardReadString();

            if ("1".trim().equalsIgnoreCase(option)) {
                if(typeOfIntrusion.equalsIgnoreCase("window")){
                    System.out.println("\t [+]Window detected!\n");
                    postSecurityBreach("WINDOW");
                }
                if(typeOfIntrusion.equalsIgnoreCase("door")){
                    System.out.println("\t [+]Door breach detected!\n");
                    postSecurityBreach("DOOR");
                }
                if(typeOfIntrusion.equalsIgnoreCase("motion")){
                    System.out.println("\t [+]Motion detected!\n");
                    postSecurityBreach("MOTION");
                }
            } else if ("X".trim().equalsIgnoreCase(option)) {
                System.out.println("\t [+]Closing the sensor.!\n");
                this.closing = true;
                try {
                    em.UnRegister();
                    Thread.sleep(1000);
                } catch (Exception e) {
                    System.out.println("\t[-]Error unregistering: " + e);
                }
                System.exit(0);
            }else {
                System.out.println("Not a number or a valid option!");
            }
        }
    }

    private void postSecurityBreach(String sensorType)
    {
        // Here we create the event.

        Event evt = new Event( -6, sensorType );

        // Here we send the event to the event manager.

        try
        {
            em.SendEvent( evt );
            //mw.WriteMessage( "Sent Window break Event" );

        } // try

        catch (Exception e)
        {
            System.out.println( "Error Posting Security Event : :" + e );

        } // catch

    } // postSecurityBreach


    public void setDone(boolean done) {
        this.done = done;
    }

    public boolean isClosing() {
        return closing;
    }
}
